<template>
  <el-drawer
    :title="drawerName === 'add' ? '添加租户' : '修改租户'"
    :visible.sync="isShowDrawer"
    direction="rtl"
    @open="handleDrawerOpen"
    @close="handleDrawerClose"
  >
    <el-form ref="roleForm" :model="formData" label-width="80px" size="mini">
      <el-form-item label="用户名" v-if="drawerName === 'add'">
        <el-input v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" v-if="drawerName === 'add'">
        <el-input show-password v-model="formData.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="formData.nickname"></el-input>
      </el-form-item>
      <el-form-item label="微信号">
        <el-input v-model="formData.wechat"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formData.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="formData.email"></el-input>
      </el-form-item>
      <el-form-item label="身份证">
        <el-input v-model="formData.idCard"></el-input>
      </el-form-item>
      <el-form-item label="等级">
        <el-input-number
          v-model="formData.level"
          :min="1"
          :max="999"
          label="租户等级"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入备注"
          v-model="formData.remark"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="confirmBtnClick"
          >确定</el-button
        >
        <el-button size="mini" @click="resetBtnClick">重置</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
export default {
  props: {
    drawerName: {
      type: String,
      default: "",
    },
    alterFormData: {
      type: Object,
      default: {},
    },
    currentDomainId: {
      type: Number | String,
      default: "",
    },
  },
  data() {
    return {
      isShowDrawer: false,
      formData: {},
    };
  },
  watch: {
    drawerName(newValue) {
      if (newValue) {
        this.isShowDrawer = true;
      } else {
        this.isShowDrawer = false;
      }
    },
    alterFormData(newValue) {
      this.formData = JSON.parse(JSON.stringify(newValue));
    },
  },
  methods: {
    handleDrawerOpen() {},
    handleDrawerClose() {
      this.$emit("alterDrawerClose");
    },
    confirmBtnClick() {
      if (this.drawerName === "add") {
        this.formData.type = -1;
        this.formData.domainId = this.currentDomainId;
        this.$emit("createTenantEvent", this.formData);
      } else if (this.drawerName === "alter") {
        this.$emit("alterTenantEvent", this.formData);
      }
    },
    resetBtnClick() {
      if (this.drawerName === "add") {
        this.formData = {};
      } else {
        this.formData = JSON.parse(JSON.stringify(this.alterFormData));
      }
    },
  },
};
</script>

<style scoped>
.el-form {
  margin-right: 10px;
}
</style>
